 <?php
    /**
     * This is a completely custom QControl and it is also a composite control,
     * meaning it utilizes several individual QControls (e.g. a QLabel and two
     * QButtons) to make one larger control.
     */
 
    class AutoCompleteList extends QControl {
        // Our SubControls
        public    $pnl;
        public    $lst;
        public    $txt;
        public    $btn;

        protected $blnUseAjax = false;
        protected $strPadding = '10px';
        // Let's Override the Default Style Settings
        protected $strWidth = '200px';
        protected $strFontSize = '36px';
        protected $blnFontBold = true;
        protected $strBackColor = '';

        protected $blnIsBlockElement = true;

        protected $objParentObject;

        public    $bolDuplicate=true;
        public    $strCallList;
        public    $strReverseCall;
        public    $strGetId;

        // We want to override the constructor in order to setup the subcontrols
        public function __construct($objParentObject, $strControlId = null) {
            // First, call the parent to do most of the basic setup
            try {
                parent::__construct($objParentObject, $strControlId);
            } catch (QCallerException $objExc) {
                $objExc->IncrementOffset();
                throw $objExc;
            }
            // Define the Controls
            $this->objParentObject=$objParentObject;

            $this->pnl = new QePanel($this);
            $this->pnl->Width= 210;
            $this->pnl->Height= 90;
            $this->pnl->BackColor= "";
            //$this->pnl->Position =  QPosition::Absolute;
            $this->pnl->Overflow= "auto";

            $this->pnl->Template = "/Applications/MAMP/htdocs/crm/crm-ev-dev/apps/www/includes/controles/AutoCompleteList.tpl.php";
            
            
            $this->lst = new QListBox($this);
            $this->lst->Rows = 4;
            $this->lst->Width = $this->strWidth;
            $this->lst->AddAction(new QFocusEvent(), new QAjaxControlAction($this,'lst_Focus'));

            $this->txt = new QAjaxAutoCompleteTextBox($this, 'txt_Change');
           
            $this->txt->MustMatch=true;
            $this->txt->Width= "120";
            $this->txt->AddAction(new QFocusEvent(), new QAjaxControlAction($this,'txt_Focus'));
            
            $this->btn = new QButton($this);
            $this->btn->Text = 'Add';
            
            $objSubmitListItemActions = array(
                new QToggleEnableAction($this->btn, false),
                new QToggleEnableAction($this->txt, false),
                new QToggleEnableAction($this->lst, false),
                new QAjaxControlAction($this,'AddListItem')
            );

            // Let's add this set of actions to the Add Button
            $this->btn->AddActionArray(new QClickEvent(500), $objSubmitListItemActions);

            // Let's add this set of actions to the Textbox, as a EnterKeyEvent
            //$this->txt->AddActionArray(new QEnterKeyEvent(), $objSubmitListItemActions);

            //Terminte enter key event
            $this->txt->AddAction(new QEnterKeyEvent(), new QTerminateAction());
            $this->lst->Enabled = true;
        }
        public function lst_Focus($strParameter){
            if ( $this->btn->Text == "Add")
                $this->btn->Text= "Remove";
        }
        public function txt_Focus($strParameter){
            if ( $this->btn->Text == "Remove")
                $this->btn->Text= "Add";
        }
        public function txt_Change($strParameter){
            $objMemberArray = $this->objParentObject->{$this->strCallList}($strParameter);
            return $objMemberArray;
	}



        public function AddListItem() {

            if ( $this->btn->Text == "Add"){
                if (strlen(trim($this->txt->Text)) == 0) {
                    //$this->txt->Warning = 'Nothing was entered';
                    $blnToReturn=false;
                    $this->txt->Focus();
                    //$this->txt->Blink();
                }else{
                    $id = $this->txt->Value;

                    if ($this->bolDuplicate == true ){
                        $foundDuplicate = false;
                        $imax = $this->lst->__get("ItemCount");
                        for ($i=0; $i <$imax; $i++){
                            $indexValue= $this->lst->GetItem($i);
                            if ( $indexValue->Value  == $id){
                                $foundDuplicate = true;
                                break;
                            }
                        }
                        if ($foundDuplicate == false ){
                            $this->lst->AddItem(trim($this->txt->Text),$id);
                        }
                        else{
                            QApplication::ExecuteJavaScript("alert('Duplicate item not allowed.');");
                        }
                        $this->txt->Text = '';
                    }
                    else{
                        $this->lst->AddItem(trim($this->txt->Text), $id);
                        $this->txt->Text = '';
                    }
                }
            }
            elseif( $this->btn->Text == "Remove"){
                   $SelectedIndex = $this->lst->SelectedIndex ;
                   if ( $SelectedIndex <> -1)
                    $this->lst->RemoveItem( $SelectedIndex);

            }

                // Let's re-enable all the controls;
                $this->txt->Enabled = true;
                $this->lst->Enabled = true;
                $this->btn->Enabled = true;
        }
       
        public function ParsePostData() {}


        public function Validate() { return true;
        }

        // Now, for the fun part -- we get to define how our sample control gets rendered
        protected function GetControlHtml() {
            // Lets get Style attributes
            $strStyle = $this->GetStyleAttributes();
            if ($this->strPadding)
                $strStyle .= sprintf('padding:%s;', $this->strPadding);
            $strStyle = sprintf('style="%s;text-align:center;"', $strStyle);
            //$this->pnl->SetCustomStyle("display", "block");

            // Lets get all the other attributes -- because we have actions defined internally,
            // we specifically do not want to include externally defined actions.  Therefore,
            // we pass in "false" for the optional blnIncludeAction parameter
            $strAttributes = $this->GetAttributes(true, false);
            
            // Lets get the rendered subcontrols -- remember to use FALSE for "blnDisplayOutput"
            /*
            $strItem = $this->txt->Render(false);
            $strAdd =  $this->btn->Render(false);
            $strListBox = $this->lst->Render(false);
            */
            $strPanel = $this->pnl->Render(false);

            // Let's render it out
            return sprintf ('%s'
                            ,$strPanel);
            /*
            return sprintf('<div id="%s" %s%s >
                                <table border="0">
                                    <tr><td>%s %s</td></tr>
                                    <tr><td>%s</td></tr>
                                </table>
                            </div>',
                $this->strControlId,
                $strStyle,
                $strAttributes,
                $strItem,
                $strAdd,
                $strListBox);
             
             */
        }

        // And our public getter/setters
        public function __get($strName) {
            switch ($strName) {
                case 'Padding': return $this->strPadding;
                case 'UseAjax': return $this->blnUseAjax;
                default:
                    try {
                        return parent::__get($strName);
                    } catch (QCallerException $objExc) {
                        $objExc->IncrementOffset();
                        throw $objExc;
                    }
            }
        }

        public function __set($strName, $mixValue) {
            // Whenever we set a property, we must set the Modified flag to true
            $this->blnModified = true;

            try {
                switch ($strName) {
                    case 'Padding': return ($this->strPadding = QType::Cast($mixValue, QType::String));
                    case 'UseAjax':
                        $blnToReturn = ($this->blnUseAjax = QType::Cast($mixValue, QType::Boolean));

                        // Whenever we change UseAjax, we must be sure to update our two buttons
                        // and their defined actions.
                        $this->SetupButtonActions();

                        return $blnToReturn;

                    default:
                        return (parent::__set($strName, $mixValue));
                }
            } catch (QCallerException $objExc) {
                $objExc->IncrementOffset();
                throw $objExc;
            }
        }
    }
?> 